package uk.ac.cam.ccn.sac.modules.audio.message;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Thomas
 */
public class AudioElement extends JFrame{
    private final JButton capture = new JButton("Capture");
    private final JButton stop = new JButton("Stop");
    private final JButton play = new JButton("Play");
    private final JButton send = new JButton("Send");
    public AudioElement(){
        super("Audio Message");
        this.setSize(200, 160);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container content = getContentPane();
        content.setLayout(new FlowLayout());
        
        capture.setEnabled(true);
        stop.setEnabled(false);
        play.setEnabled(false);
        send.setEnabled(false);
        
        
        ActionListener captureListener = 
        new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                capture.setEnabled(false);
                stop.setEnabled(true);
                play.setEnabled(false);
                send.setEnabled(false);
                AudioChatManager.getInstance().startRecord();
           }
        };
        capture.addActionListener(captureListener);
        content.add(capture);
        
        ActionListener stopListener = 
        new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                capture.setEnabled(true);
                stop.setEnabled(true);
                play.setEnabled(true);
                send.setEnabled(true);
                AudioChatManager.getInstance().stopRecord();
           }
        };
        stop.addActionListener(stopListener);
        content.add(stop);
        
        ActionListener playListener = 
        new ActionListener() {
            @Override
                public void actionPerformed(ActionEvent e) {
                    AudioChatManager.getInstance().playRecorded();
                }
            };
        play.addActionListener(playListener);
        content.add(play);
        
        ActionListener sendListener = 
        new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                capture.setEnabled(true);
                stop.setEnabled(false);
                play.setEnabled(false);
                send.setEnabled(false);
                AudioChatManager.getInstance().send();
            }
        };
        send.addActionListener(sendListener);
        content.add(send);
        pack();
        setVisible(true);
    }
}
